package com.jw.blog.wblog.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jw.blog.annotation.SystemLog;
import com.jw.blog.utils.ConstantUtil;
import com.jw.blog.wblog.entity.BlogType;
import com.jw.blog.wblog.entity.custom.ResultBean;
import com.jw.blog.wblog.service.IBlogTypeService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wan Email-wlf18726994755@163.com
 * @version 1.0
 * @className BlogTypeControllerAdmin
 * @date 2020/6/21 15:05
 **/
@RestController
public class BlogTypeControllerAdmin {
    @Resource
    private IBlogTypeService iBlogTypeService;

    @PostMapping("/admin/addBlogType")
    @SystemLog(description = ConstantUtil.BLOGTYPE_ADD, userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> addBlogType(String prarm,BlogType blogType){
        QueryWrapper<BlogType> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<BlogType> eq = wrapper.lambda().eq(BlogType::getTypeName,blogType.getTypeName());
        ResultBean<Object> resultBean = new ResultBean<>();
        if (ObjectUtil.isNotNull(iBlogTypeService.getOne(eq))) {
            resultBean.setCode(2);
            return resultBean;
        }
        blogType.setAddTime(LocalDateTime.now());
        if (iBlogTypeService.save(blogType)) {
            resultBean.setCode(ResultBean.SUCCESS);
        } else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    @PostMapping("/admin/deleteBlogType")
    @SystemLog(description = ConstantUtil.BLOGTYPE_DELETE, userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> deleteBlogType(String prarm,@RequestParam(value = "id") Integer id){
        ResultBean<Object> resultBean = new ResultBean<>();
        if (iBlogTypeService.getById(id).getNum() == 0){
            if (iBlogTypeService.removeById(id)) {
                resultBean.setCode(ResultBean.SUCCESS);
            }else {
                resultBean.setCode(ResultBean.FAIL);
            }
        }else {
            resultBean.setCode(2);
            resultBean.setMsg("不知道什么原因，但就是失败了");
        }

        return resultBean;
    }

    @PostMapping("/admin/selectBlogTypeListByPage")
    public ResultBean<IPage<BlogType>> selectBlogTypeListByPage(
            BlogType blogType,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        Page<BlogType> blogTypePage = new Page<>();
        blogTypePage.setCurrent(page);
        blogTypePage.setSize(pageSize);
        ResultBean<IPage<BlogType>> resultBean = new ResultBean<>();
        Page<BlogType> typePage = iBlogTypeService.page(blogTypePage);
        if (typePage.getRecords().size() != 0){
            resultBean.setCode(ResultBean.SUCCESS);
            resultBean.setData(typePage);

        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    @PostMapping("/admin/selectBlogTypeById")
    public ResultBean<BlogType> selectBlogTypeById(@RequestParam(value = "id") Integer id) {
        ResultBean<BlogType> resultBean = new ResultBean<>();
        BlogType blogType = iBlogTypeService.getById(id);
        if (ObjectUtil.isNotNull(blogType)){
            resultBean.setCode(ResultBean.SUCCESS);
            resultBean.setData(blogType);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    @PostMapping("/admin/updateBlogType")
    @SystemLog(description = ConstantUtil.BLOGTYPE_UPDATE, userType = ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> updateBlogType(String prarm, BlogType blogType) {
        QueryWrapper<BlogType> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<BlogType> eq = wrapper.lambda().eq(BlogType::getTypeName, blogType.getTypeName());
        ResultBean<Object> resultBean = new ResultBean<>();
        if (ObjectUtil.isNotNull(iBlogTypeService.getOne(eq))) {

            resultBean.setCode(2);
            return resultBean;
        }

        if (iBlogTypeService.updateById(blogType)) {
            resultBean.setCode(ResultBean.SUCCESS);
        } else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

}
